﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Quanlybanhang
{
    public partial class DonviForm : Office2007Form
    {
        public DonviForm()
        {
            InitializeComponent();
            panelThem.Hide();
            panelXoa.Hide();
            panelChinhsua.Hide();
            khoitao();
            panelsua();
            panelxoa();
        }

        //Phuong thuc khoi tao cac gia tri ban dau
        public void khoitao()
        {
            //Lay danh sach nhung chu hang ban hang - kieu = 2
            string cmb_cmd = "select tendonvi as 'Tên thùng hàng', soluongtrung as 'Số lượng trứng' from donvi";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridViewX1.DataSource = dt;   
        }

        public void panelsua()
        {
            //Dua vao cmbbox
            string cmb_cmd = "select distinct tendonvi from donvi";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            pSua_cmbTen.DataSource = dt;
            pSua_cmbTen.DisplayMember = "tendonvi";
        }

        public void panelxoa()
        {
            //Dua vao cmbbox
            string cmb_cmd = "select distinct tendonvi from donvi";
            SqlDataAdapter sda = new SqlDataAdapter(cmb_cmd, MainForm.con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            pXoa_cmbTen.DataSource = dt;
            pXoa_cmbTen.DisplayMember = "tendonvi";
        }

        public int get_Xoa_ID(String str)
        {
            int sua_id = 0;
            String cmd = "select id from donvi where tendonvi = '" + pXoa_cmbTen.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                sua_id = (int)(rdr["id"]);
            }
            rdr.Close();
            return sua_id;
        }

        public int get_Sua_ID(String str)
        {
            int sua_id = 0;
            String cmd = "select id from donvi where tendonvi = '" + pSua_cmbTen.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                sua_id = (int)(rdr["id"]);
            }
            rdr.Close();
            return sua_id;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            khoitao();
        }

        private void btnThemmoi_Click(object sender, EventArgs e)
        {
            panelThem.Show();
            panelXoa.Hide();
            panelChinhsua.Hide();
        }

        private void btnChinhsua_Click(object sender, EventArgs e)
        {
            panelsua();
            panelThem.Hide();
            panelXoa.Hide();
            panelChinhsua.Show();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            panelxoa();
            panelThem.Hide();
            panelXoa.Show();
            panelChinhsua.Hide();
        }

        private void pThem_btnXoa_Click(object sender, EventArgs e)
        {
            pThem_ten.Text = "";
            PThem_sotrung.Text = "";
        }

        private void pThem_btnAdd_Click(object sender, EventArgs e)
        {
            int sotrung = 0;
            string trung = "";
            try
            {
                trung = PThem_sotrung.Text;
                sotrung = Convert.ToInt32(trung);
                if (sotrung < 0)
                    MessageBox.Show("Hãy nhập đúng số lượng trứng");
                else
                {
                    try
                    {
                        SqlCommand sqlComm = new SqlCommand();
                        sqlComm = MainForm.con.CreateCommand();
                        sqlComm.CommandText = @"INSERT INTO DONVI VALUES('" + pThem_ten.Text + "', " + sotrung + ")";
                        sqlComm.ExecuteNonQuery();
                        MessageBox.Show("Thêm mới thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Thêm mới không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch(Exception ee){MessageBox.Show("Hãy nhập đúng số lượng trứng");};            
        }

        private void pSua_btnXoa_Click(object sender, EventArgs e)
        {
            pSua_txtTen.Text = "";
            pSua_Soluong.Text = "";
        }

        private void pSua_cmbTen_SelectedIndexChanged(object sender, EventArgs e)
        {
            String ten = ""; int sotrung = 0;
            String cmd = "select tendonvi, soluongtrung from donvi where tendonvi = '" + pSua_cmbTen.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                ten = (string)(rdr["tendonvi"]);
                sotrung = (int)(rdr["soluongtrung"]);
            }
            pSua_txtTen.Text = ten;
            pSua_Soluong.Text = sotrung.ToString();
            rdr.Close();
        }

        private void pSua_btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand sqlComm = new SqlCommand();
                sqlComm = MainForm.con.CreateCommand();
                sqlComm.CommandText = @"UPDATE DONVI SET tendonvi ='" + pSua_txtTen.Text + "', soluongtrung='" + pSua_Soluong.Text + "' WHERE id='" + get_Sua_ID(pSua_cmbTen.Text) + "'";
                sqlComm.ExecuteNonQuery();
                panelsua();
                khoitao();
                MessageBox.Show("Lưu lại thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ee)
            {
                MessageBox.Show("Lưu lại không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void pXoa_btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult xoa = MessageBox.Show("Bạn có chắc chắn xóa loại thùng hàng này?", "Xóa thùng hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (xoa == DialogResult.Yes)
            {
                try
                {                
                    SqlCommand sqlComm = new SqlCommand();
                    sqlComm = MainForm.con.CreateCommand();
                    sqlComm.CommandText = @"DELETE DONVI WHERE id='" + get_Xoa_ID(pXoa_cmbTen.Text) + "'";
                    sqlComm.ExecuteNonQuery();
                    panelxoa();
                    khoitao();
                    MessageBox.Show("Xóa thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Xóa không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void pXoa_cmbTen_SelectedIndexChanged(object sender, EventArgs e)
        {
            int trung = 0;
            String ten = ""; string sotrung = "";
            String cmd = "select tendonvi, soluongtrung from donvi where tendonvi = '" + pSua_cmbTen.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(cmd, MainForm.con);
            SqlDataReader rdr = sqlcmd.ExecuteReader();
            while (rdr.Read())
            {
                ten = (string)(rdr["tendonvi"]);
                trung = (int)(rdr["soluongtrung"]);
            }
            sotrung = trung.ToString();
            rdr.Close();
        }

    }
}
